#!/usr/bin python3
# -*- coding:UTF-8 -*-
# Author: nigo
import plotly.graph_objects as go
import numpy as np

def normdis(x,mu, sigma):
    """正态分布概率密度函数
    x:区间
    mu:平均值
    sigma:标准差
    return:概率密度
    """
    p = np.exp(-((x - mu)**2) / (2* sigma**2)) / (sigma * np.sqrt(2*np.pi))
    return p


if __name__ == "__main__":
    #读取数据
    data = np.random.randn(50000)
    data_contract = np.random.randn(50000)
    mean = np.mean(data) # 平均值
    std = np.std(data) # 标准差
    max = np.max(data) # 最大值
    min = np.min(data) # 最小值
    x= np.arange(min,max,0.01) # 构造区间
    y = normdis(x,mean,std) # 计算正态分布概率密度
    # 轨迹
    histogram = go.Histogram(x=data,histnorm='probability density',name='概率密度',marker_color='#EB89B5')
    histogram_contract = go.Histogram(x=data_contract,histnorm='probability density',name='概率密度对比',marker_color='#330C73')
    line = go.Scatter(x=x,y=y,mode="lines",name='正态分布概率密度')
    # 画布
    fig = go.Figure([histogram,histogram_contract,line])
    # 更新样式
    fig.update_layout(bargap=0.2,bargroupgap=0.1)
    # 显示画布
    fig.show()
